from weakref import WeakValueDictionary

class Connection(object):
    def open(self, url):
        raise NotImplementedError("This protocol does not support this operation")
    
    def is_open(self):
        raise NotImplementedError("This protocol does not support this operation")
    
    def exists(self, path):
        raise NotImplementedError("This protocol does not support this operation")

    def create_file(self, path):
        raise NotImplementedError("This protocol does not support this operation")
        
    def open_file(self, path, mode):
        raise NotImplementedError("This protocol does not support this operation")

    def close(self):
        raise NotImplementedError("This protocol does not support this operation")
        
    def iter_folder(self, path):
        raise NotImplementedError("This protocol does not support this operation")
    
class ConnectionFactory(object):
    def __init__(self):
        self.connections = {}
        
    def get_connection(self, url, connection_type):
        if url.scheme not in self.connections:
            self.connections[url.scheme] = WeakValueDictionary()
        dict = self.connections[url.scheme]
        
        if url.authority in dict:
            return dict[url.authority]
        else:
            connection = connection_type()
            dict[url.authority] = connection
            return connection
        
connection_factory = ConnectionFactory()